Imports OpenAccess

Public Class Form1

    Private Sub cmdCreateData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateData.Click
        Dim scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope
        Dim parent As Parent
        Dim child As Child
        Dim car As Car
        Dim ws1, ws2 As WorkShop

        Me.Cursor = Cursors.WaitCursor
        scope.Transaction.Begin()
        ws1 = New WorkShop
        ws2 = New WorkShop
        For i As Integer = 0 To 9
            parent = New Parent
            parent.Name = "Parent " & i.ToString
            For j As Integer = 0 To 4
                child = New Child
                child.Name = "AChild " & j.ToString
                parent.AChilds.Add(child)
                If j Mod 2 = 0 Then
                    ws2.KidMembers.Add(child)
                Else
                    ws1.KidMembers.Add(child)
                End If
            Next
            For j As Integer = 0 To 4
                child = New Child
                child.Name = "BChild " & j.ToString
                parent.BChilds.Add(child)
            Next
            For j As Integer = 0 To 1
                car = New Car
                car.Marke = "Marke " & j.ToString
                parent.Cars.Add(car)
            Next
            If i Mod 2 = 0 Then
                ws2.AdultMembers.Add(parent)
            Else
                ws1.AdultMembers.Add(parent)
            End If
            scope.Add(parent)
            scope.Add(ws1)
            scope.Add(ws2)
        Next
        scope.Transaction.Commit()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Dim scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope
        Me.Cursor = Cursors.WaitCursor
        scope.Transaction.Begin()
        For Each p As Parent In Query.Parents(scope)
            For Each c As Child In p.AChilds
                scope.Remove(c)
            Next
            For Each c As Child In p.BChilds
                scope.Remove(c)
            Next
            scope.Remove(p)
        Next
        scope.Transaction.Commit()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub cmdRemoveWS1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemoveWS1.Click
        Dim scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope
        scope.Transaction.Begin()
        Dim workshops As IQueryResult = Query.Workshops(scope)
        If workshops.GetEnumerator.Count > 0 Then scope.Remove(workshops(0))
        scope.Transaction.Commit()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As SomeBusinessObject = New SomeBusinessObject
        obj.Text = "Hallo"
        With ObjectScopeProvider1.GetNewObjectScope
            .Transaction.Begin()
            .Add(obj)
            .Transaction.Commit()
        End With
    End Sub

End Class
